package fm.dongman.modellib.remote;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import org.jetbrains.annotations.NotNull;
import org.json.JSONObject;

import java.util.HashMap;

import fm.dongman.animefm.contractlib.IModel;
import fm.dongman.animefm.contractlib.contract.LoginContract;
import fm.dongman.modellib.integrated.QQWBWXLoginHelper;
import fm.dongman.modellib.RemoteHelper;
import fm.dongman.modellib.StaticArea.StaticUrl;
import fm.dongman.modellib.model.UserModel;
import fm.dongman.modellib.source.LoginRepository;

/**
 * 网络加载用户信息
 * Created by KUIGE on 2017/10/13.
 */

public class LoginRemoteDataSource implements LoginContract.ILoginDataSource {
    // 实例
    private static LoginRemoteDataSource INSTANCE;

    public static LoginRemoteDataSource getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new LoginRemoteDataSource();
        }
        return INSTANCE;
    }

    private LoginRemoteDataSource() {
    }

    @Override
    public void login(@NotNull String userName, @NotNull String pwd, @NotNull LoadSourceCallback<? super IModel.IUserModel> callback) {
        HashMap<String, String> map = new HashMap<>();
        map.put("account", userName);
        map.put("password", pwd);
        JSONObject jSONObject = RemoteHelper.Companion.getInstance().parsePostUrl(StaticUrl.LOGIN_REMOTE, map);
        if (jSONObject == null) {
            callback.onDataLoadFiled();
            return;
        }
        UserModel model = RemoteOperationHelper.getOperationHelper().downUser(jSONObject);
        callback.onDataLoaded(model);
    }

    @Override
    public void loginSocial(@NotNull Activity activity, int type, @NotNull final LoadSourceCallback<? super IModel.IUserModel> callback) {
        if (type == LoginRepository.TYPE_LOGIN_QQ) {
            // qq 登录
            loginTecentQQ(activity, callback);
            return;
        }
        if (type == LoginRepository.TYPE_LOGIN_SINA) {
            // 新浪登录
            loginSinaWB(activity, callback);
            return;
        }
        if (type == LoginRepository.TYPE_LOGIN_WX) {
            // 微信登录
            loginTecentWX(activity, callback);
            return;
        }
        callback.onDataLoadFiled();
    }

    /**
     * 微信登录
     *
     * @param activity activity
     * @param callback 回调
     */
    private void loginTecentWX(@NotNull Activity activity, @NotNull final LoadSourceCallback<? super IModel.IUserModel> callback) {
        // 微信登录
        QQWBWXLoginHelper.INSTANCE.doWXLogin(activity, new QQWBWXLoginHelper.LoginResultCallback() {
            @Override
            public void onCompletedLogin(@NotNull QQWBWXLoginHelper.LoginUserModel user) {
                // 登录成功
                HashMap<String, Object> map = new HashMap<>();
                map.put("service_name", user.getMServiceName());
                map.put("uid", user.getMUid());
                map.put("gender", user.getMGender());
                map.put("name", user.getMNickName());
                map.put("avatar_url", user.getMAvatarUrl());
                loginAnimeFM(map, callback);
            }

            @Override
            public void onCancelOrError(@NotNull String msg) {
                Log.i(getClass().getName(), "登录失败！ 原因：" + msg);
                callback.onDataLoadFiled();
            }
        });
    }

    /**
     * 新浪微博登录
     *
     * @param activity activity
     * @param callback 回调
     */
    private void loginSinaWB(@NotNull Activity activity, @NotNull final LoadSourceCallback<? super IModel.IUserModel> callback) {
        // 新浪登录
        QQWBWXLoginHelper.INSTANCE.doSinaWBLogin(activity, new QQWBWXLoginHelper.LoginResultCallback() {
            @Override
            public void onCompletedLogin(@NotNull QQWBWXLoginHelper.LoginUserModel user) {
                // 登录成功
                HashMap<String, Object> map = new HashMap<>();
                map.put("service_name", user.getMServiceName());
                map.put("uid", user.getMUid());
                loginAnimeFM(map, callback);
            }

            @Override
            public void onCancelOrError(@NotNull String msg) {
                Log.i(getClass().getName(), "登录失败！ 原因：" + msg);
                callback.onDataLoadFiled();
            }
        });
    }

    /**
     * qq 登录
     *
     * @param activity activity
     * @param callback 回调
     */
    private void loginTecentQQ(@NotNull Activity activity, @NotNull final LoadSourceCallback<? super IModel.IUserModel> callback) {
        // QQ 登录
        QQWBWXLoginHelper.INSTANCE.doQQLogin(activity, new QQWBWXLoginHelper.LoginResultCallback() {
            @Override
            public void onCompletedLogin(@NotNull QQWBWXLoginHelper.LoginUserModel user) {
                // 登录成功
                HashMap<String, Object> map = new HashMap<>();
                map.put("service_name", user.getMServiceName());
                map.put("uid", user.getMUid());
                map.put("gender", user.getMGender());
                map.put("name", user.getMNickName());
                map.put("avatar_url", user.getMAvatarUrl());
                loginAnimeFM(map, callback);
            }

            @Override
            public void onCancelOrError(@NotNull String msg) {
                Log.i(getClass().getName(), "登录失败！ 原因：" + msg);
                callback.onDataLoadFiled();
            }
        });
    }

    /**
     * 登录到 dongman.fm
     */
    private void loginAnimeFM(HashMap<String, Object> map, @NotNull LoadSourceCallback<? super IModel.IUserModel> callback) {
        JSONObject jsonObject = RemoteHelper.Companion.getInstance().parsePostUrl(StaticUrl.OTHER_LOGIN, map);
        if (jsonObject == null) {
            callback.onDataLoadFiled();
            return;
        }
        callback.onDataLoaded(RemoteOperationHelper.getOperationHelper().downUser(jsonObject));
    }

    @Override
    public void getData(@NotNull LoadSourceCallback<? super IModel.IUserModel> callback) {
    }

    @Override
    public void saveData(IModel.IUserModel dataModel) {
    }

    @Override
    public void refreshData() {
    }

    @Override
    public void offLine() {
    }

    public void refreshD(JSONObject object) {
        RemoteOperationHelper.getOperationHelper().downUser(object);
    }
}
